package com.eminem.controller.dept;

import com.eminem.common.StrMath;
import com.eminem.controller.BaseServlet;
import com.eminem.model.Conference;
import com.eminem.service.DeptService;
import com.eminem.service.impl.DeptServiceimpl;
import com.eminem.utils.PageUtil;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/dept/*")
public class DeptController extends BaseServlet {

    private DeptService deptService = new DeptServiceimpl();

    public void list(HttpServletRequest req, HttpServletResponse resp){
        try {
            List<Map<String , Object>> mapList =  deptService.getDeptList();
            req.getSession().setAttribute("mapList",mapList);
            req.getRequestDispatcher("/html/department.jsp").forward(req,resp);

        }catch (Exception e){

        }
    }


    public void system(HttpServletRequest req, HttpServletResponse resp){
        try {
            String page = req.getParameter("page");
            String name = req.getParameter("name");
//            System.out.println(name);
            String userId = getUserId(req);
            int currentPage = 1;
            if (null != page){
                currentPage = Integer.parseInt(page);
            }
            List<Map<String,Object>> dept = deptService.getDeptName();
            req.getSession().setAttribute("dept",dept);
            List<Map<String,Object>> list = deptService.getDeptSystemList(userId,currentPage,name);
            req.getSession().setAttribute("list",list);

            long count = deptService.getSystemCount(name);
            //总分页
            long pageTotal = PageUtil.PageTotal(count);
            long startPage = PageUtil.getStartPage(currentPage);
//            System.out.println(startPage);
            //结束页
            long endPage = PageUtil.getEndPage(currentPage, count);
//            System.out.println(endPage);
            req.setAttribute("currentPage",currentPage);
            req.setAttribute("count",count);
            req.setAttribute("name",name);
            req.setAttribute("pageTotal",pageTotal);
            req.setAttribute("pageCount", StrMath.PAGE_COUNT);
            req.setAttribute("startPage",startPage);
            req.setAttribute("endPage",endPage);
            req.getRequestDispatcher("/html/meeting.jsp").forward(req,resp);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void show(HttpServletRequest req, HttpServletResponse resp){
        try {
            String id = req.getParameter("id");
            Map<String, Object> mapList = deptService.getSystemContent(id);
            if (null != mapList){
                req.getSession().setAttribute("mapList",mapList);
                req.getRequestDispatcher("/html/meeting_detail.jsp").forward(req,resp);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void AddMeeting(HttpServletRequest req, HttpServletResponse resp){
        try {
            String page = req.getParameter("page");
            String title = req.getParameter("title");
            String dept = req.getParameter("dept");
            String date = req.getParameter("date");
            String content = req.getParameter("content");
            Conference conference = new Conference();
            conference.setTitle(title);
            conference.setDeptName(dept);
            conference.setContent(content);
//            conference.setStartTime(date);
//            conference.setPublishDate((Date) date);

            resp.sendRedirect("/html/meeting_add.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
